package prototype.phase2.ui;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.jmex.audio.stream.AudioInputStream;


class DynamicAudioStream extends AudioInputStream {
    private int samplesPerSecond = 44100;

    public DynamicAudioStream() throws IOException {
        // supply a dummy URL for the superclass
        super(DynamicAudioStream.class.getClassLoader().getResource("."),
                10.0f);
    }

    @Override
    public int available() {
        return samplesPerSecond;
    }

    @Override
    public int getBitRate() {
        return getChannelCount() * getDepth() / 8 * samplesPerSecond;
    }

    @Override
    public int getChannelCount() {
        return 1;
    }

    @Override
    public int getDepth() {
        return 16;
    }

    @Override
    public AudioInputStream makeNew() throws IOException {
        return this;
    }

    /** synthesizes the next audio buffer */
    @Override
    public int read(ByteBuffer b, int offset, int length)
            throws IOException {
        for (int i = offset; i < length; i++) {
            b.put(i, (byte) (Math.random() * 200));
        }
        return length;
    }
}